package com.xjoyt.pay.merchant.register.services;

import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.merchant.register.po.Merchant;
import com.xjoyt.pay.merchant.register.repository.MerchantRepository;

@Service
public class MerchantService{

	@Autowired
	private MerchantRepository repository;


	/*@Autowired
	private MerchantContract merchantContract;*/

	/*@LockMethod
	public ExecuteResult<?> register(@LockParam({ "distributor.id", "outNo" }) Merchant param) {
		if (param.distributor == null || StringUtils.isBlank(param.distributor.id)) {
			return ExecuteResult.fail("代理商不能为空");
		}
		Distributor distributor = distributorRepository.findOne(param.distributor.id);
		if (distributor == null) {
			return ExecuteResult.fail("代理商不存在");
		}

		Merchant entity = repository.findOneByDistributorIdAndNo(distributor.id, param.outNo);
		if (entity != null && BooleanUtils.isTrue(entity.enabled)) {
			return ExecuteResult.fail("无法重复注册");
		}
		entity = entity == null ? new Merchant() : entity;
		String id = entity.id;
		ObjectUtils.fieldCopy(param, entity);
		entity.id = id;
		entity.enabled = null;
		entity.addedDate = new Date();
		repository.save(entity);
		return ExecuteResult.ok();
	}*/

	/*@LockMethod(block = true)
	public ExecuteResult<?> update(
			@LockParam({ "distributor.id", "outNo" }) com.xjoyt.pay.merchant.register.po.Merchant param) {
		if (param.distributor == null || StringUtils.isBlank(param.distributor.id)) {
			return ExecuteResult.fail("代理商不能为空");
		}
		Distributor distributor = distributorRepository.findOne(param.distributor.id);
		if (distributor == null) {
			return ExecuteResult.fail("代理商不存在");
		}

		Merchant entity = repository.findOneByDistributorIdAndNo(distributor.id, param.outNo);
		if (entity == null) {
			return ExecuteResult.fail("商户不存在");
		}
		if (BooleanUtils.isNotTrue(entity.enabled)) {
			return ExecuteResult.fail("无法修改非正式商户");
		}

		String id = entity.id;
		Date addedDate = entity.addedDate;
		if (addedDate == null)
			addedDate = new Date();
		ObjectUtils.fieldCopy(param, entity);
		entity.id = id;
		entity.enabled = null;
		entity.addedDate = addedDate;
		repository.save(entity);
		return ExecuteResult.ok();
	}*/
	
	/*public PageResult<Merchant> loadPage(PageParam<MerchantPageParam> param) {
		return repository.loadPage(param)null;
	}*/

	/*public ExecuteResult<?> saveValue(String id, Entry<String, String> param) throws Exception {
		if ("id".equals(param.getKey())) {
			return ExecuteResult.fail("无法修改id");
		}
		Merchant entity = repository.findOne(id);
		if (entity == null) {
			return ExecuteResult.fail("数据不存在");
		}
		if (entity.enabled != null) {
			return ExecuteResult.fail("数据已提交，无法修改");
		}
		Field field = entity.getClass().getField(param.getKey());
		Object convert = ConvertUtils.convert(param.getValue(), field.getType());
		field.set(entity, convert);
		repository.save(entity);
		return ExecuteResult.ok();
	}*/

	public ExecuteResult<?> register(Merchant merchant) {
		
		repository.save(merchant);
		return ExecuteResult.ok();
	}
	
	public List<Merchant> findAll() {
		return repository.findAll();
	}
	
	public Merchant findOne(String id) {
		return repository.findOne(id);
	}
	
	public ExecuteResult<?> update(Merchant merchant){
		
		return ExecuteResult.ok();
	}
	
	public ExecuteResult<?> delete(){
		
		return ExecuteResult.ok();
	}
	public ExecuteResult<?> check(String id) {
		
		Merchant merchant = repository.findOne(id);
		  if (merchant == null)
				return ExecuteResult.fail("商户不存在");

		  if (BooleanUtils.isNotTrue(merchant.enabled))
					return ExecuteResult.fail("商户已被停用");

		 /* if (merchant.distributor == null)
					return ExecuteResult.fail("代理商不存在");

		  if (BooleanUtils.isNotTrue(merchant.enabled))
					return ExecuteResult.fail("代理商已被停用");

		  if (merchant.distributor.company == null)
					return ExecuteResult.fail("所属机构不存在");

		  if (BooleanUtils.isNotTrue(merchant.distributor.company.enabled))
					return ExecuteResult.fail("机构已被停用");*/
		  
		/*  MerchantRate merchantRate = merchantRateRepository.findOneByMerchantIdAndType(merchant.id , payType);
				if (merchantRate == null)
					return ExecuteResult.fail("商户费率未设置");
				putObject(merchantRate, args);

		 DistributorRate distributorRate = distributorRateRepository.findOneByDistributorIdAndType(merchant.distributor.id , payType);
				if (distributorRate == null)
					return ExecuteResult.fail("代理商费率未设置");
				putObject(distributorRate, args);*/
				
		return ExecuteResult.ok();
	}
}
